<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Article extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('article');
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$status = intval($this->input->post('status'));
		$title = $this->input->post('title');
		$where[1] = 1;

		if ($id) {
			$where['id'] = $id;
		}

		if ($status) {
			$where['status'] = $status;
		}

		if ($title) {
			$where['title like'] = '%' . $title . '%';
		}

		$result['total'] = $this->db->where($where)->count_all_results('article');
		$start = ($page - 1) * $rows;
		$result['rows'] = $this->db->where($where)->limit($rows, $start)->get('article')->result_array();

		foreach ($result['rows'] as $k => $v) {
			$class = $this->db->where('id', $v['cid'])->get('article_class')->row_array();
			$result['rows'][$k]['class'] = $class['name'];
			$result['rows'][$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
		}

		echo json_encode($result);
	}

	public function selectdata()
	{
		$cid = intval($this->uri->segment(3));
		$result = $this->db->where_in('mod', array('list', 'page'))->get('article_class')->result_array();

		foreach ($result as $k => $v) {
			$note[$k]['id'] = $v['id'];
			$note[$k]['text'] = $v['name'];

			if ($cid == $v['id']) {
				$note[$k]['selected'] = true;
			}
		}

		echo json_encode($note);
	}

	public function add()
	{
		$this->load->view('article_add');
	}

	public function insert()
	{
		$title = $this->input->post('title');
		$short_title = $this->input->post('short_title');
		$en_short_title = $this->input->post('en_short_title');
		$cid = $this->input->post('cid');
		$sort = $this->input->post('sort');
		$content = $this->input->post('content');
		$status = intval($this->input->post('status'));
		$en_title = $this->input->post('en_title');
		$en_content = $this->input->post('en_content');
		$data = array('title' => $title, 'short_title' => $short_title, 'en_short_title' => $en_short_title, 'sort' => $sort, 'cid' => $cid, 'status' => $status, 'content' => $content, 'en_title' => $en_title, 'en_content' => $en_content, 'add_time' => time());
		$rs = $this->db->insert('article', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$this->db->where('id', $id);
		$rs = $this->db->get('article');
		$d = $rs->row_array();
		$this->load->view('article_edit', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$title = $this->input->post('title');
		$short_title = $this->input->post('short_title');
		$en_short_title = $this->input->post('en_short_title');
		$cid = $this->input->post('cid');
		$sort = $this->input->post('sort');
		$content = $this->input->post('content');
		$status = intval($this->input->post('status'));
		$en_title = $this->input->post('en_title');
		$en_content = $this->input->post('en_content');
		$data = array('title' => $title, 'short_title' => $short_title, 'en_short_title' => $en_short_title, 'sort' => $sort, 'cid' => $cid, 'status' => $status, 'content' => $content, 'en_title' => $en_title, 'en_content' => $en_content, 'add_time' => time());
		$this->db->where('id', $id);
		$rs = $this->db->update('article', $data);
		$this->ajax_result($rs, 'update');
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$this->db->where_in('id', $id);
			$rs = $this->db->delete('article');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}
}

?>
